package com.yihu.cms.service;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.caucho.hessian.client.HessianProxyFactory;
import com.yihu.beans.Artwork;
import com.yihu.beans.ArtworkAuthor;
import com.yihu.beans.ArtworkCategory;
import com.yihu.beans.ArtworkType;
import com.yihu.beans.CommodityStates;
import com.yihu.beans.PictureDetail;
import com.yihu.beans.Users;
import com.yihu.beans.searchCondition.ArtworkPageConditions;
import com.yihu.beans.searchCondition.PutOnShelvesTime;
import com.yihu.dataService.common.DateUtil;
import com.yihu.dataService.hessian.mongodb.interfaceService.ArtworkHService;
import com.yihu.dataService.hessian.resource.ResourceMongoDBManager;

public class ArtworkService {

	public int add(Artwork artworkJson) {
		int result = 0;
		try {
			ArtworkHService artwork = (ArtworkHService) ResourceMongoDBManager
					.getSingleton().getBeans("artworkHSC");
			result = artwork.addArtwork(artworkJson);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 1;
			e1.printStackTrace();
		}
		return result;
	}

	public int del(String id) {
		int result = 0;
		try {
			ArtworkHService artwork = (ArtworkHService) ResourceMongoDBManager
					.getSingleton().getBeans("artworkHSC");
			result = artwork.delArtwork(id);
		} catch (MalformedURLException e) {
			result = 1;
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 2;
			e1.printStackTrace();
		}
		return result;
	}

	public int update(Artwork artworkJson) {
		int result = 0;
		try {
			ArtworkHService artwork = (ArtworkHService) ResourceMongoDBManager
					.getSingleton().getBeans("artworkHSC");
			result = artwork.updateArtwork(artworkJson);
		} catch (MalformedURLException e) {
			result = 1;
			System.out.println(e.toString());
		} catch (IOException e1) {
			result = 2;
			e1.printStackTrace();
		}
		return result;
	}

	public Artwork getById(String id) {
		Artwork artwork = null;
		try {
			ArtworkHService artworkHS = (ArtworkHService) ResourceMongoDBManager
					.getSingleton().getBeans("artworkHSC");
			artwork = artworkHS.getArtworkById(id);
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return artwork;
	}

	public List<Artwork> getPaging(int skip, int limit, ArtworkPageConditions pc) {
		List<Artwork> result = new ArrayList<Artwork>();
		try {
			ArtworkHService artwork = (ArtworkHService) ResourceMongoDBManager
					.getSingleton().getBeans("artworkHSC");

			result = artwork.getArtworkPaging(skip, limit, pc);
			System.out.println(artwork.toString());
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		System.out.println("end..");
		return result;
	}

	public long getTotal() {
		long result = 0;
		try {
			ArtworkHService artwork = (ArtworkHService) ResourceMongoDBManager
					.getSingleton().getBeans("artworkHSC");
			result = artwork.getArtworkTotal();
		} catch (MalformedURLException e) {
			System.out.println(e.toString());
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return result;
	}

	public static void main(String args[]) {
		ArtworkService as = new ArtworkService();

//		Artwork artwork = new Artwork();
//		Users user = new Users();
//		user.setId("1");
//		user.setName("xiaoliu");
//
//		ArtworkAuthor aa = new ArtworkAuthor();
//		aa.setId((long) 2);
//		aa.setName("董杰");
//
//		ArtworkCategory ac = new ArtworkCategory();
//		ac.setId(1);
//		ac.setName("油画");
//
//		CommodityStates cs = new CommodityStates();
//		cs.setId(1);
//		cs.setName("已上架");
//
//		ArtworkType at = new ArtworkType();
//		at.setId(2);
//		at.setName("茶席艺术品");
//
//		// artwork.setId(2);
//		artwork.setName("深蓝系列--山");
//		artwork.setUser(user);
//		artwork.setPrice(8999.99);
//		artwork.setCreateTime(new Date());
//		artwork.setFinishTime(new Date());
//		artwork.setArtworkAuthor(aa);
//		artwork.setArtworkCategory(ac);
//		artwork.setCommodityStates(cs);
//		artwork.setLike(5);
//		artwork.setIsRecommend(7);
//		artwork.setSorts(8);
//		artwork.setPv(100);
//		artwork.setArtworkType(at);
//		artwork.setContent("123456");
//
//		List<PictureDetail> pictureU = new ArrayList<PictureDetail>();
//		PictureDetail pd1 = new PictureDetail();
//		pd1.setId("123");
//		pd1.setHeight(1000);
//		pd1.setWidth(800);
//		PictureDetail pd2 = new PictureDetail();
//		pd2.setId("1234");
//		pd2.setHeight(1000);
//		pd2.setWidth(800);
//		pictureU.add(pd1);
//		pictureU.add(pd2);
//
//		artwork.setPictures(pictureU);
//
//		String str = JSON.toJSONString(artwork);
//		System.out.println(str);
//		System.out.println(as.add(artwork));

		// get
		// System.out.println(as.getById(18));

		// update
		// String artwork = as.getArtworkById(1);
		//
		// Artwork a = JSON.parseObject(artwork, Artwork.class);
		//
		// a.setPrice(2000.00);
		//
		// System.out.println(as.updateArtwork(JSON.toJSONString(a)));

		// System.out.println(JSON.toJSONString(as.getArtworkPaging(1, 8)));

		// paging
		ArtworkPageConditions pc = new ArtworkPageConditions();
		// pc.setName("大雪");
//		pc.setArtworkAuthorName("国");
		// pc.setCommodityStatesName("已下架");
		// pc.setArtworkTypeId(2);
		// Prices ps = new Prices();
		// ps.setMinPrices(100);
		// ps.setMaxPrices(550000.99);
		// pc.setPrice(ps);

		// PutOnShelvesTime pst = new PutOnShelvesTime();
		// pst.setBeginPutOnShelvesTime(DateUtil.stringToDate("2016-1-1"));
		// pst.setEndPutOnShelvesTime(DateUtil.stringToDate("2016-1-26"));
		// pc.setPst(pst);
		pc.setCommodityCategoryId("4");
		System.out.println(JSON.toJSONString(as.getPaging(1, 3, pc)));
		
//		System.out.println("artwork json = " + JSON.toJSONString(as.getById("5732dab4bd128b8c57cbdb6e")));

		// System.out.println(as.delArtwork(1));
		//
		// System.out.println(JSON.toJSONString(as.getArtworkPaging(1, 8)));

		// get
		// System.out.println(JSON.toJSONString(as.getArtworkById(2)));
		System.out.println("end");
	}

}
